package com.sj.stone.controller;

import com.sj.stone.domain.oracle.AdminRepository;
import com.sj.stone.domain.oracle.OrderSignForRepository;
import com.sj.stone.domain.oracle.SalesManRepository;
import com.sj.stone.domain.oracle.entry.Admin;
import com.sj.stone.domain.oracle.entry.OrderSignfor;
import com.sj.stone.domain.oracle.entry.SalesMan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private SalesManRepository salesManRepository;
    @Autowired
    private OrderSignForRepository orderSignForRepository;

    @RequestMapping("/findAdminId")
    @ResponseBody
    public String findAdminId(String username) {

        Admin admin = adminRepository.findAllByUsername(username.trim());

        return "姓名:"+admin.getTruename()+",电话:"+admin.getMobilephone()+",ID:"+admin.getId();
    }

    @RequestMapping("/upsal")
    @ResponseBody
    public String upsal(String oldMobile, String newMobile) {
        Admin admin = null;
        SalesMan salesMan = null;
        String cbbc = "手机号修改成功";
        try {
            admin = adminRepository.findByMobilephone(oldMobile.trim());
            salesMan = salesManRepository.findByMobile(oldMobile.trim());
        } catch (Exception e) {
            cbbc = "手机号不唯一";
            e.printStackTrace();
        }
        if (admin != null && salesMan != null) {
            admin.setMobilephone(newMobile.trim());
            salesMan.setMobile(newMobile.trim());
            adminRepository.save(admin);
            salesManRepository.save(salesMan);
            List<OrderSignfor> oslist = orderSignForRepository.findByUserPhone(oldMobile.trim());
            for(int i =0;i<oslist.size();i++){
                oslist.get(i).setUserPhone(newMobile.trim());
                orderSignForRepository.save(oslist.get(i));
            }

        } else {
            cbbc = "手机号输入错误";
        }

        return cbbc;
    }
}
